﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using CRMWeb.Utilities.Context;
using Newtonsoft.Json;


namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for EmailTemplate
    /// </summary>
    public class EmailTemplate : BaseHandler
    {
        
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        public void DisplayTemplateNode(string Company)
        {
           
            try
            {
                bool isSMS = false;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetTemplateCategoryNodesList(isSMS, false, false, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        public void DisplayCategoryList(string Company)
        {
            try
            {
                bool isSMS = false;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetTemplateCategoryNodesList(isSMS, true, false, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        public void GetEmailTemplateContentById(int EmailTemplateId)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetEmailTemplateContent(EmailTemplateId));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void EmailTemplateShowAll()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(EmailTemplateHandler.BindEmailTemplate());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void getColumnName()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(EmailTemplateHandler.getColumnName());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public int EmailTemplateUpdate(int EmailId, string TemplateFormat, string emailSubject, int CategoryID, string EmailCompany)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = EmailTemplateHandler.UpdateEmailTemplate(EmailId, TemplateFormat, emailSubject, CategoryID, EmailCompany);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public int UpdateEmailTemplateDual(int EmailId, string TemplateFormat,int CategoryID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = EmailTemplateHandler.UpdateEmailTemplateDual(EmailId, TemplateFormat,CategoryID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public int EmailTemplateAdd(string EmailTemplate, string TemplateFormat, string emailSubject, int CategoryID, string EmailCompany)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = EmailTemplateHandler.AddEmailTemplate(EmailTemplate, TemplateFormat, emailSubject, CategoryID, EmailCompany);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public bool EmailTemplateDelete(int EmailId)
        {
            bool flag = false;
            try
            {
                flag = EmailTemplateHandler.DeleteEmailTemplate(EmailId);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        public void SelectAllEmailId(string Company)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(EmailTemplateHandler.BindEmailId(Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void SelectPasswordDef()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(EmailTemplateHandler.SelectPassword());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }


        //Display email dialog template nodes:
        public void DisplayTemplateNodeForDialog(string Company)
        {
            try
            {
                bool isSMS = false;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetCategoryNodeForDialog(isSMS, false, false, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Display email data list:
        public void GetEmailDataList(string Company,int CategoryId)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = EmailTemplateHandler.GetEmailDataList(pageIndex, pageSize, Company,CategoryId);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Delete list data:
        public bool DeleteListData(int EmailId)
        {
            bool flag = false;
            try
            {
                flag = EmailTemplateHandler.DeleteListData(EmailId);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }
    }
}